#!/bin/sh

#plots the last histogram or the one speciefied by
#offset given as first commandline argument.
#an offset if zero plots the last
#an offset of one, plots the one before the last and so on...


#Gnu Plot Script File
GPSF=plot.gp

no=0

if [ -n "$1" ]; then  no=$1; fi

for filename in `ls -tb hist_*.dat`
do
  if [ $no -le 0 ]; then break; fi
  no=$[$no - 1];
done

echo set pointsize 0.4 >$GPSF

print="$2"
if [ "$print" == "print" ]
then
  print=true
  #set terminal postscript [orientation] fontsize
  echo set terminal postscript portrait 10 >>$GPSF
  echo set output \"${filename%dat}ps\" >>$GPSF
else
  print=false
  echo set terminal x11 >$GPSF
fi



echo set multiplot >>$GPSF
echo set size 1, 0.3 >>$GPSF

echo set origin 0, 0 >>$GPSF
echo plot [12450:13050] [0:1400] \"$filename\" u 1:2 >>$GPSF
echo set origin 0, 0.3 >>$GPSF
echo plot [8000:8300] [0:1400] \"$filename\" u 1:3 >>$GPSF
echo set origin 0, 0.6 >>$GPSF
echo plot [7300:7600] [0:1400] \"$filename\" u 1:4 >>$GPSF

echo unset multiplot >>$GPSF

if [ $print == "false" ]
then
    echo pause -1 >>$GPSF
fi

gnuplot plot.gp

lpr ${filename%dat}ps